package com.tubang.organization.shiro;

import static com.google.common.base.Strings.emptyToNull;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Optional;

/**
 * Created by yangqj on 2017/4/30.
 */
@Service
public class ShiroService {
	
    @Autowired
    private ShiroFilterFactoryBean shiroFilterFactoryBean;
    
    /**
     * 初始化权限
     */
    public Map<String, String> loadFilterChainDefinitions() {
        // 权限控制map.从数据库获取
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/css/**","anon");
        filterChainDefinitionMap.put("/js/**","anon");
        filterChainDefinitionMap.put("/img/**","anon");
        filterChainDefinitionMap.put("/font-awesome/**","anon");
        filterChainDefinitionMap.put("/**", "authc");
        return filterChainDefinitionMap;
    }

    /**
     * 重新加载权限
     */
    public void updatePermission() {

        synchronized (shiroFilterFactoryBean) {

            AbstractShiroFilter shiroFilter = null;
            try {
                shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean
                        .getObject();
            } catch (Exception e) {
                throw new RuntimeException(
                        "get ShiroFilter from shiroFilterFactoryBean error!");
            }

            PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter
                    .getFilterChainResolver();
            DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver
                    .getFilterChainManager();

            // 清空老的权限控制
            manager.getFilterChains().clear();

            shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
            shiroFilterFactoryBean
                    .setFilterChainDefinitionMap(loadFilterChainDefinitions());
            // 重新构建生成
            Map<String, String> chains = shiroFilterFactoryBean
                    .getFilterChainDefinitionMap();
            for (Map.Entry<String, String> entry : chains.entrySet()) {
                String url = entry.getKey();
                String chainDefinition = entry.getValue().trim()
                        .replace(" ", "");
                manager.createChain(url, chainDefinition);
            }

            System.out.println("更新权限成功！！");
        }
    }

    /**
     * 根据userId 清除当前session存在的用户的权限缓存
     * @param userIds 已经修改了权限的userId
     */
   /* public void clearUserAuthByUserId(List<Integer> userIds){
        if(null == userIds || userIds.size() == 0)	return ;
        //获取所有session
        Collection<Session> sessions = redisSessionDAO.getActiveSessions();
        //定义返回
        List<SimplePrincipalCollection> list = new ArrayList<SimplePrincipalCollection>();
        for (Session session:sessions){
            //获取session登录信息。
            Object obj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            if(null != obj && obj instanceof SimplePrincipalCollection){
                //强转
                SimplePrincipalCollection spc = (SimplePrincipalCollection)obj;
                //判断用户，匹配用户ID。
                obj = spc.getPrimaryPrincipal();
                if(null != obj && obj instanceof User){
                    User user = (User) obj;
                    System.out.println("user:"+user);
                    //比较用户ID，符合即加入集合
                    if(null != user && userIds.contains(user.getId())){
                        list.add(spc);
                    }
                }
            }
        }
        RealmSecurityManager securityManager =
                (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm realm = (MyShiroRealm)securityManager.getRealms().iterator().next();
        for (SimplePrincipalCollection simplePrincipalCollection : list) {
            realm.clearCachedAuthorizationInfo(simplePrincipalCollection);
        }
    }*/
    
    
    
    public static void main(String[] args) {
    	    
//		Optional<Integer> possible = Optional.of(6);
//		if (possible.isPresent()) {
//			System.out.println("possible isPresent:" + possible.isPresent());
//			System.out.println("possible value:" + possible.get());
//		}
//		Optional<Integer> possible1 = Optional.of(6);
//		Optional<Integer> absentOpt = Optional.absent();
//		Optional<Integer> NullableOpt = Optional.fromNullable(null);
//		Optional<Integer> NoNullableOpt = Optional.fromNullable(10);
//        if(possible1.isPresent()){
//            System.out.println("possible isPresent:"+possible1.isPresent());
//            System.out.println("possible value:"+possible1.get());
//        }
//        if(absentOpt.isPresent()){
//            System.out.println("absentOpt isPresent:"+absentOpt.isPresent()); ;
//        }
//        if(NullableOpt.isPresent()){
//            System.out.println("fromNullableOpt isPresent:"+NullableOpt.isPresent()); ;
//        }
//        if(NoNullableOpt.isPresent()){
//            System.out.println("NoNullableOpt isPresent:"+NoNullableOpt.isPresent()); ;
//        }
        
        List<String> list = new LinkedList<String>();  
        list.add("A");  
        list.add(null);  
        list.add("C");  
        list.add(null);  
        list.add("E");  
        list.add("");  
        Optional<String> possible;  
        for(int i = 0 ; i < list.size(); i++){  
        	System.out.println("----------------------");
        	System.out.println("emptyToNull(list.get(i)),"+ emptyToNull(list.get(i)));
            possible = Optional.fromNullable(emptyToNull(list.get(i)));  
            System.out.println("index: "+i+" -  value: "+possible.orNull());  
        }  
        
        
	}
    
}
